#include "datamonitoringanalysis.h"
#include <QDir>
#include <QFileInfo>
#include <QFileDevice>
#include <QSet>
#include <QCoreApplication>
#include <QSettings>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonParseError>

#include "filereadutil.h"



DataMonitoringAnalysis::DataMonitoringAnalysis(QObject *parent) : QObject(parent)
{

    QString  m_qstrFileName = QCoreApplication::applicationDirPath() + "/config/MinddlewareConfig.ini";
    QSettings * m_psetting = new QSettings(m_qstrFileName, QSettings::IniFormat);
    QVariant qvar = m_psetting->value(QString("/%1/%2").arg("INFO").arg("dir"));
    QVariant qtitle = m_psetting->value(QString("/%1/%2").arg("INFO").arg("title"));
    this->tilte=qtitle.toString();
    QVariant baddir = m_psetting->value(QString("/%1/%2").arg("INFO").arg("baddir"));

    watcher =new WatcherFile(this);
    connect(watcher, SIGNAL(fileChange(QString)), this, SLOT(fileUpdated(QString)));
    watcher->batpath=baddir.toString();
    watcher->addWatchDir(qvar.toString());


    qDebug()<<"path:"<< qvar.toString();

}


///
/// \brief unitWeightReadFile
/// \param path
///读取容重结构文件
void DataMonitoringAnalysis::unitWeightReadFile(const QString& path){
    QStringList datas=  FileReadUtil::readFile(path);
    if(datas.length()==0)
        return;

    QString outdata =  unitWeightAnalysisString(datas);
    qDebug()<< "put "<< outdata;
    emit flushMsg(outdata);

}

///
/// \brief unitWeightAnalysisString
/// \param datas
/// \return
///容重解析结构
///
QString DataMonitoringAnalysis::unitWeightAnalysisString(QStringList datas){

    QJsonParseError parseJsonerr;

    double totob=0;
    QString itemname;
    QString product;
    int number=0;
    QString datetime;
    QStringList allvalue;

    for(int i=0;i<datas.length();i++){
        QJsonDocument jdoc = QJsonDocument::fromJson(datas[i].toUtf8(),&parseJsonerr);
        if(parseJsonerr.error!=QJsonParseError::NoError){
            qDebug()<<"解析错误:"<<datas[i];
            return QString();
        }
        QJsonObject json =jdoc.object();
        itemname= json["itemname"].toString();
        product= json["product"].toString();
        number= json["number"].toInt();
        datetime= json["datetime"].toString();
        double v= json["value"].toDouble();
        totob +=v;
        allvalue.append(QString::number(v)) ;

    }
    double tempValue = totob/datas.length();

    QJsonObject jsonv;
    jsonv.insert("itemname",itemname);
    jsonv.insert("product",product);
    jsonv.insert("number",number);
    jsonv.insert("datetime",datetime);
    jsonv.insert("value",tempValue);
    jsonv.insert("values",allvalue.join(","));

    QJsonObject jsona;
    jsona.insert("title",tilte);
    jsona.insert("code",0);
    jsona.insert("data",jsonv);


    QJsonDocument doc;
    doc.setObject(jsona);

    QByteArray ba =doc.toJson(QJsonDocument::Compact);
    QString dstr(ba);
    return dstr;

}

///
/// \brief unitWeightReadFile
/// \param path
///读取水分结构文件
void DataMonitoringAnalysis::waterReadFile(const QString& path){
    qDebug()<<"读取水分结构";
    QStringList datas=  FileReadUtil::readFile(path);
    if(datas.length()==0)
        return;

    QString outdata = waterAnalysisString(datas);
    qDebug()<< "put "<< outdata;
    emit flushMsg(outdata);

}
///
/// \brief waterAnalysisString
/// \param datas
/// \return
///水分解析结构
QString DataMonitoringAnalysis::waterAnalysisString(QStringList datas){
    QJsonParseError parseJsonerr;

    double totob=0;
    QString itemname;
    QString product;
    int number=0;
    QString datetime;
    QStringList allvalue;

    for(int i=0;i<datas.length();i++){
        QJsonDocument jdoc = QJsonDocument::fromJson(datas[i].toUtf8(),&parseJsonerr);
        if(parseJsonerr.error!=QJsonParseError::NoError){
            qDebug()<<"解析错误:"<<datas[i];
            return QString();
        }

        QJsonObject json =jdoc.object();
        product= json["ProductName"].toString();
        QString numberv= json["SampleName"].toString();
        QString ay = numberv.mid(0, numberv.length()-8);
        ay=ay.replace(product,"");
        number =ay.toInt();

        datetime= json["Time"].toString();
        QJsonArray datas = json.value("Data").toArray();
        QJsonObject dataobject= datas.at(0).toObject();
        itemname= dataobject["Name"].toString();
        double v= dataobject["Value"].toDouble();
        totob +=v;
        allvalue.append(QString::number(v)) ;

    }
    double tempValue = totob/datas.length();

    QJsonObject jsonv;
    jsonv.insert("itemname",itemname);
    jsonv.insert("product",product);
    jsonv.insert("number",number);
    jsonv.insert("datetime",datetime);
    jsonv.insert("value",tempValue);
    jsonv.insert("values",allvalue.join(","));

    QJsonObject jsona;
    jsona.insert("title",tilte);
    jsona.insert("code",0);
    jsona.insert("data",jsonv);


    QJsonDocument doc;
    doc.setObject(jsona);

    QByteArray ba =doc.toJson(QJsonDocument::Compact);
    QString dstr(ba);
    return dstr;
}

///
/// \brief DataMonitoringAnalysis::fileUpdated
/// \param path
///路径下文件发生改变
void DataMonitoringAnalysis::fileUpdated(const QString &path){
    qDebug()<<"  updated:"<< path;
    if(this->tilte=="water")
        waterReadFile(path);
    else
        unitWeightReadFile(path);

}
